package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.bank.exceptions.NotEnoughFundsException;
import com.acme.bankapp.domain.bank.exceptions.OverDraftLimitExceededException;

/**
 * Class implements Checking type of Account with overdraft possibility
 * Constructor expects nonNegative overdraft
 * @author Vasilyev Anton
 *
 */
public class CheckingAccount extends AbstractAccount implements Account {
	private double overdraft;

	public CheckingAccount(double balance, double overdraft) throws IllegalArgumentException {
		super(balance);
		if (overdraft < 0)
			throw new IllegalArgumentException("Negative overdraft");
		this.overdraft = overdraft;
	}
	
	public CheckingAccount(double balance) {
		this(balance, 0.0);
	}

	public double maxAmountToWithdraw() {
		return this.balance + this.overdraft;
	}

	public double getOverdraft() {
		return overdraft;
	}

	public void setOverdraft(double overdraft) {
		this.overdraft = overdraft;
	}

	@Override
	public void withdraw(double amount) throws NotEnoughFundsException {
		if (this.balance + this.overdraft >= amount) {
			this.balance -= amount;
		} else {
			throw new OverDraftLimitExceededException(amount, "NotEnoughFunds: available "
														+ this.maxAmountToWithdraw());
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		long temp;
		temp = Double.doubleToLongBits(overdraft);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (Double.doubleToLongBits(overdraft) != Double.doubleToLongBits(other.getOverdraft()))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CheckingAccount [overdraft=" + overdraft + ", balance="
				+ balance + "]";
	}
	
	

}
